package com.laborder.sysyy.controller.college;


import com.laborder.sysyy.common.Constant;
import com.laborder.sysyy.entity.College;
import com.laborder.sysyy.entity.User;
import com.laborder.sysyy.service.college.CollegeService;
import com.laborder.sysyy.service.log.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping(value = "college")
public class CollegeController {
	@Autowired
	private CollegeService collegeService;
	@Autowired
	private LogService logService;

	@GetMapping("toCollege")
	public String toCollege(Model model){

		return "/college/college";
	}
	@GetMapping("toCollegeAdd")
	public String toCollegeAdd(Model model){

		return "/college/collegeAdd";
	}
	@ResponseBody
	@RequestMapping("/findCollegeWithPage")
	public Map<String,Object> findCollegeWithPage(int page,int limit,String clName){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("page", (page-1)*limit);
		map.put("limit", limit);
		map.put("clName", clName);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code",0);
		resultMap.put("msg","");
		resultMap.put("count",collegeService.getCollegeCount(map));
		resultMap.put("data",collegeService.findCollegeWithPage(map));
		return resultMap;
	}
	@ResponseBody
	@RequestMapping("/saveCollegeInfo")
	public int saveCollegeInfo(College college,HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
           User user = (User) session.getAttribute("user");
		int flag = 0;
		College collegeByName = collegeService.findCollegeByName(college.getClName());
		if(collegeByName==null){
			flag = collegeService.saveCollegeInfo(college);
			logService.save(user.getUserName(),Constant.ADD_COLLEGE,request);
		}else{
			flag = -1;
		}

		
		return flag;
	}
	@ResponseBody
	@RequestMapping("/delCollegeInfo")
	public int delCollegeInfo(int clId,HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User) session.getAttribute("user");
		int flag = 0;
		try {

				flag = collegeService.delCollegeInfo(clId);
			logService.save(user.getUserName(),Constant.DEL_COLLEGE,request);
		} catch (Exception e) {
			flag =  -1;
		}
		
		
		return flag;
	}

	@RequestMapping("/updateCollegeInfo")
	public int updateCollegeInfo(College college,HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User) session.getAttribute("user");
		int flag = 0;
		logService.save(user.getUserName(),Constant.UPDATE_COLLEGE,request);
		return collegeService.updateCollegeInfo(college);
	}
	
}